﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Engine.NetClasses
{
    public class ServerComand
    {
        public string Description;
        public Action action;
        public ServerComand(string Description,Action action)
        {
            this.Description = Description;
            this.action = action;
        }
    }
    public class ServerInput
    {
        private ServerLogs m_serverLog;
        private ServerOptions m_serverOptions;
        private ServerState m_serverState;
        private BaseServer baseServer;
        private Server baseServerold;
        private Dictionary<string, ServerComand> command = new Dictionary<string, ServerComand>();

        public ServerInput(ServerLogs m_serverLog, ServerOptions m_serverOptions, ServerState m_serverState, BaseServer baseServer)
        {
            // TODO: Complete member initialization
            this.m_serverLog = m_serverLog;
            this.m_serverOptions = m_serverOptions;
            this.m_serverState = m_serverState;
            this.baseServer = baseServer;
            addBaseComand();
        }

        private void addBaseComand()
        {
            command.Add("-help", new ServerComand("Show help", new Action(showHelp)));
            command.Add("-?", new ServerComand("Show help", new Action(showHelp)));
            command.Add("-option", new ServerComand("Show option window", new Action(m_serverOptions.ShowForm)));
            command.Add("-status", new ServerComand("Show option window", new Action(m_serverState.ShowForm)));
            command.Add("-stop", new ServerComand("Stop server", new Action(StopServer)));
            command.Add("-restart", new ServerComand("Restart server", new Action(RestartServer)));
        }
        public ServerInput(ServerLogs m_serverLog, ServerOptions m_serverOptions, ServerState m_serverState,Server old)
        {
            // TODO: Complete member initialization
            this.m_serverLog = m_serverLog;
            this.m_serverOptions = m_serverOptions;
            this.m_serverState = m_serverState;
            this.baseServerold = old;
            addBaseComand();
            //this.baseServer = baseServer;
        }
        void addComand(String name, ServerComand cmd)
        {
            if (!command.ContainsKey(name))
            {
                command.Add(name, cmd);
            }
            
        }
        void showHelp()
        {
            Console.WriteLine("------------------------HELP-------------------------");
            Console.WriteLine("Commands:");
            foreach (var item in command)
            {
                Console.WriteLine("{0}:   {1}",item.Key,item.Value.Description);
            }
            Console.WriteLine("-----------------------------------------------------");
        }
        void StopServer()
        {
            if (baseServerold != null)
            {
                baseServerold.serverRun = false;
            }
            if (baseServer!=null)
            {
                 baseServer.ShutDown();
            }
           
        }
        void RestartServer()
        {
            if (baseServer != null)
            {
                baseServer.Restart();
            }
        }
        public void DoCommand(string name)
        {
            if (command.ContainsKey(name.ToLower()))
            {
                command[name.ToLower()].action.Invoke();
            }
        }
        public void doinput(string NameCmd)
        {
            DoCommand(NameCmd.ToLower());
        }
    }
}
